$(document).ready(
    function(){
        
        var UI = {};
        (function (UI) {
            
            UI.start = function(){
//                $.when(Actions.GetUserList({action : "test"})).then(
//                    function(data){
//                        var html = "";
//                        var row = null;
//                        for(var i = 0; i<data.length; i++){
//                            row = data[i];
//                            var accountName = "Unknown";
//                            if(row.account_type === "0"){
//                                accountName = "Private";
//                            } else if(row.account_type === "1"){
//                                accountName = "Business";
//                            }
//                            html += "<tr id='row-" + (i + 1) + "'>";
//                            html += "<td id='username-"+(i+1)+"'>" + row.username + "</td>";
//                            html += "<td id='email-"+(i+1)+"'>" + row.email + "</td>";
//                            html += "<td id='firstname-"+(i+1)+"'>" + row.first_name + "</td>";
//                            html += "<td id='lastname-"+(i+1)+"'>" + row.last_name + "</td>";
//                            html += "<td id='password-"+(i+1)+"'>" + row.password + "</td>";
//                            html += "<td id='accounttype-"+(i+1)+"'>" + accountName + "</td>";
//                            html += "</tr>";
//                        }
//                        $("#new-user-grid").append(html);
//                    }
//                );
            };
            
        }(UI));
        var Helpers = {};
        (function (Helpers) {
            
            
        }(Helpers));
        
        var Actions = {};
        (function (Actions) {
            
            Actions.addUsersInsert = function(args) {
                console.log("%c runSQL addUsersInsert", "color: green");
                var dfd = new $.Deferred();
                $.ajax({
                    type: "POST",
                    url: "../resources/helpers/addUsersInsert.php",
                    data: args,
                    cache: false,
                    success: function(data){
                        console.log("%c Returned from addUsersInsert:","color: green");
                        console.log(data);
//                        data = JSON.parse(data);
//                        dfd.resolve(data);
                        dfd.resolve();
                    }
                });
                return dfd.promise();
            };
            
            Actions.GetUserList = function(args) {
                var dfd = new $.Deferred();
                console.log("getUserList");
                console.log(args);
                $.ajax({
                    type: "GET",
                    url: "../resources/helpers/addUsersGetUserList.php",
                    data: args,
                    cache: false,
                    success: function(data){
                        data = JSON.parse(data);
                        console.log(data);
                        dfd.resolve(data);
                    }
                });
                return dfd.promise();
            };
            
        }(Actions));
        
        var Controller = {};
        (function (Controller) {
            
            function runSqlClicked(){
                var rowCount = $('#new-user-grid tr').length;
                
                
                for(var i=1; i<rowCount; i++){
                    var row = $("#row-" + i);
                    if(row.html()){
                        var args ={
                            action      : "test",
                            username    : row.find("#username-"+i).html(),
                            email       : row.find("#email-"+i).html(),
                            firstname   : row.find("#firstname-"+i).html(),
                            lastname    : row.find("#lastname-"+i).html(),
                            password    : row.find("#password-"+i).html(),
                            status      : '1',
                            accounttype : row.find("#accounttype-"+i).html()
                        };
                        console.log(args);
                        $.when(Actions.addUsersInsert(args)).then(
                            function(){
                                
                            }
                        );
                    }
                    
                }
            }
            
            Controller.init= function(){
                $("#run-sql-btn").unbind("click").bind("click", runSqlClicked);
            };
            
        }(Controller));	
        
        UI.start();
        Controller.init();
    }
);